#include "GLSceneInterfaceDrawers.h"

#include <iostream>

namespace catan
{

/*static*/ void GLSceneInterfaceDrawers::drawRectangularTexture(float left, float right, float bottom, float top, GLuint texture, SDLCanvas &sdlCanvas)
{
	GLint oldTexture;
	glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTexture);
	float newLeft = left;
	float newRight = right;
	float newBottom = bottom;
	float newTop = top;
	float maxSize = (newRight-newLeft > newBottom-newTop ? newRight-newLeft : newBottom-newTop);
	float textureHalfWidth = 0.5f*(newRight-newLeft)/maxSize;
	float textureHalfHeight = 0.5f*(newBottom-newTop)/maxSize;
	
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
	glTexCoord2f(0.5f+textureHalfWidth, 0.5f-textureHalfHeight);
    glVertex3f(newRight, newTop, 0.0f);

	glTexCoord2f(0.5f-textureHalfWidth, 0.5f-textureHalfHeight);
    glVertex3f(newLeft, newTop, 0.0f);

	glTexCoord2f(0.5f-textureHalfWidth, 0.5f+textureHalfHeight);
    glVertex3f(newLeft, newBottom, 0.0f);

	glTexCoord2f(0.5f+textureHalfWidth, 0.5f+textureHalfHeight);
    glVertex3f(newRight, newBottom, 0.0f);
    glEnd();
    glBindTexture(GL_TEXTURE_2D, oldTexture);
}

/*static*/ void GLSceneInterfaceDrawers::drawText(const char *textString, int textLength, bool isCursorShown, float top, float left, float height, GLuint texture, SDLCanvas &sdlCanvas)
{
	GLint oldTexture;
	glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTexture);
	float newTop = top;
	float newLeft = left;
	float newHeight = height;
	float newWidth = newHeight * (16.0f/33);
	float textureCharacterWidth = 32.0f/512;
	float textureCharacterHeight = 66.0f/512;
	int lineNumber = 0;
	int iAtLastNewLine = 0;

    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
    for (int i=0; i<=textLength; ++i)
    {
    	int intValueOfCharacter;
    	if (i==textLength)
    	{
    		if (!isCursorShown)
    		{
    			break;
    		}
    		intValueOfCharacter = 127;
    	}
    	else
    	{
        	intValueOfCharacter = static_cast<int>(textString[i]);
        	if (intValueOfCharacter == 127)
        	{
        		intValueOfCharacter = 0;
        	}
        	if (intValueOfCharacter == 13 || intValueOfCharacter == 10)
        	{
        		iAtLastNewLine = i+1;
        		++lineNumber;
        		continue;
        	}
    	}
    	if (intValueOfCharacter >= 32 && intValueOfCharacter < 128)
    	{
    		int modI = i - iAtLastNewLine;
    		float xPartInTexture = (intValueOfCharacter%16)*textureCharacterWidth;
    		float yPartInTexture = (intValueOfCharacter/16-2)*textureCharacterHeight;
    		glTexCoord2f(xPartInTexture+textureCharacterWidth, yPartInTexture);
    		glVertex3f(newLeft+(modI+1)*newWidth, newTop+newHeight*lineNumber, 0.0f);
    		
    		glTexCoord2f(xPartInTexture, yPartInTexture);
    		glVertex3f(newLeft+modI*newWidth, newTop+newHeight*lineNumber, 0.0f);
    		
    		glTexCoord2f(xPartInTexture, yPartInTexture+textureCharacterHeight);
    		glVertex3f(newLeft+modI*newWidth, newTop+newHeight*(lineNumber+1), 0.0f);
    		
    		glTexCoord2f(xPartInTexture+textureCharacterWidth, yPartInTexture+textureCharacterHeight);
    		glVertex3f(newLeft+(modI+1)*newWidth, newTop+newHeight*(lineNumber+1), 0.0f);
    	}
//		glTexCoord2f(1.0f, 0.375f);
//	    glVertex3f(newRight, newTop, 0.0f);
//	
//		glTexCoord2f(0.0f, 0.375f);
//	    glVertex3f(newLeft, newTop, 0.0f);
//	
//		glTexCoord2f(0.0f, 0.625f);
//	    glVertex3f(newLeft, newBottom, 0.0f);
//	
//		glTexCoord2f(1.0f, 0.625f);
//	    glVertex3f(newRight, newBottom, 0.0f);
    }
    glEnd();
    glBindTexture(GL_TEXTURE_2D, oldTexture);
}

void GLSceneInterfaceDrawers::drawDie(int rolledNumber, int side, float left, float top, float width, GLuint texture, SDLCanvas &sdlCanvas)
{
	GLint oldTexture;
	glGetIntegerv(GL_TEXTURE_BINDING_2D, &oldTexture);
	float newLeft = left;
	float newRight = left + width;
	float newBottom = top + width*(128.0f/85);
	float newTop = top;
	float textureXOffset = (85*((rolledNumber-1)%3) + 256*(side%2))/512.0f; 
	float textureYOffset = ((rolledNumber-1)/3 + (side/2)*2)*0.25f;
	
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
	glTexCoord2f(textureXOffset+(85.0f/512.0f), textureYOffset);
    glVertex3f(newRight, newTop, 0.0f);

	glTexCoord2f(textureXOffset, textureYOffset);
    glVertex3f(newLeft, newTop, 0.0f);

	glTexCoord2f(textureXOffset, textureYOffset+0.249f);
    glVertex3f(newLeft, newBottom, 0.0f);

	glTexCoord2f(textureXOffset+(85.0f/512.0f), textureYOffset+0.249f);
    glVertex3f(newRight, newBottom, 0.0f);
    glEnd();
    glBindTexture(GL_TEXTURE_2D, oldTexture);
}

/*private*/ GLSceneInterfaceDrawers::GLSceneInterfaceDrawers()
{
}

}
